package com.cmii.sjw.department.control.jsc.controller;

import com.cmii.sjw.department.control.jsc.model.SchoolNum;
import com.cmii.sjw.department.control.jsc.model.Summary;
import com.cmii.sjw.department.control.jsc.model.vo.UniversityIndicatorVo;
import com.cmii.sjw.department.control.jsc.service.*;
import com.cmii.sjw.department.control.jsc.util.Result;
import com.cmii.sjw.department.control.jsc.vo.*;
import com.cmii.sjw.department.control.request.IRSTest;
import com.cmii.sjw.department.control.request.bean.DataRequest;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 建设高质量体系
 *
 * @author lizheng
 * @date 2025-07-04
 */
@RestController
@CrossOrigin
@RequestMapping("/building")
public class CountyIndicatorsController {

    @Resource
    private CountyIndicatorsService countyIndicatorsService;

    @Resource
    private CountyBasicInfoService countyBasicInfoService;

    @Resource
    private UniversityIndicatorsService universityIndicatorsService;

    @Resource
    private UniversityBasicInfoService universityBasicInfoService;

    @Resource
    private SchoolNumService schoolNumService;

    @Resource
    private DistrictKpiService districtKpiService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private SummaryService summaryService;

    /**
     * 区县指标
     *
     * @return 根据区县名查出对应值
     */

    @PostMapping("/indicators")
    public Result<Map<String, Map<String, String>>> listByCountyIndicators(@RequestParam String indicatorName) {
        Map<String, Map<String, String>> result = countyIndicatorsService.listByCountyIndicators(indicatorName);
        return Result.success(result);
    }

    /**
     * 区县基本情况
     *
     * @return 根据区县名查出对应的基本情况
     */

    @PostMapping("/basic")
    public Result<CountyBasicInfoVO> getByCountyName(@RequestParam String countyName) {

        CountyBasicInfoVO vo = countyBasicInfoService.getByCountyName(countyName);

        return vo != null ? Result.success(vo) : Result.error("未找到对应区县数据");

    }

    /**
     * 高校指标
     *
     * @return 根据高校名查出对应值
     */

    @PostMapping("/high")
    public Result<Map<String, Map<String, Object>>> listByUniversityIndicators(@RequestParam String highName) {
        Map<String, Map<String, Object>> result = universityIndicatorsService.listByUniversityIndicators(highName);
        return Result.success(result);
    }

    /**
     * 高校基本情况
     *
     * @return 根据高校名查出对应的基本情况
     */

    @PostMapping("/info")
    public Result<List<UniversityBasicInfoVO>> listGroupedByType(@RequestParam String universityName) {
        List<UniversityBasicInfoVO> result = universityBasicInfoService.listGroupedByType(universityName);
        return Result.success(result);
    }

    /**
     * 教师队伍
     *
     * @return 根据高校名查出对应的教师情况
     */

    @PostMapping("/teacher")
    public Result<TeacherVO> listGroupedByTeacher(@RequestParam String schoolName) {
        return teacherService.listGroupedByTeacher(schoolName);
    }

    /**
     * 区县师生学生数量
     *
     * @return 根据区县名查出对应的数量
     */

    @PostMapping("/schoolNum")
    public Result<SchoolNumVO> getSchoolNum(@RequestParam String countyName) {
        SchoolNum data = schoolNumService.getByCountyName(countyName);

        Summary summary = summaryService.getByCountyName(countyName);
        if (data == null) {
            return Result.success(null);
        }

        SchoolNumVO vo = new SchoolNumVO();
        vo.setSchoolNum(data.getSchoolNum());
        vo.setTeacherNum(data.getTeacherNum());
        vo.setStudentNum(data.getStudentNum());

        vo.setCoverArea(summary.getCoverArea());
        vo.setSchoolHouseArea(summary.getSchoolHouseArea());

        return Result.success(vo);
    }

    /**
     * 区县绩效指标
     *
     * @return 根据区县名查出对应的指标
     */
    @PostMapping("/size")
    public Result<DistrictVO> getSchoolAndClassSize(@RequestParam String districtName) {
        DistrictVO data = districtKpiService.getKpiByDistrict(districtName);
        return Result.success(data);
    }

    /**
     * 区县指标(市级)
     *
     * @return 所有区县指标
     */

    @PostMapping("/city")
    public  Result<List<Map<String, String>>> listByCityIndicators() {
        List<Map<String, String>> result = countyIndicatorsService.listByCityIndicators();
        return Result.success(result);
    }

    /**
     * IRStest
     *
     * @return test
     */
    @PostMapping("/test")
    public Result<String> test() {
        // 请求的url
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250721000028/xdata/tbl/api/execute/0f4aa2c2-9560-46a8-9afc-4ee6a49e83e9";


        // 自定义参数放到 params
        Map<String, String> params = new HashMap<>();
        params.put("districtcode", "500240");

        // 外层分页参数传进去
        DataRequest request = new DataRequest(params, 1, 50);

        String result = IRSTest.doTest(url, request);


        System.out.println("接口返回结果：" + result);

        return Result.success(result);
    }


    /**
     * 高校指标1
     * @param highName 高校名
     * @return 高校指标
     * @author jfh
     * @date 2025-07-08
     */
    @PostMapping("/gxGzljytx")
    public Result<Map<String ,String>> gxGzljytx(@RequestParam String highName) {
        Map<String ,String> success = universityIndicatorsService.gxGzljytx(highName);
        return Result.success(success);
    }
    /**
     * 科研平台
     * @param highName 高校名
     * @return 高校指标
     * @author jfh
     * @date 2025-07-08
     */
    @PostMapping("/gxKypt")
    public Result<Map<String ,Object>> gxKypt(@RequestParam String highName) {
        Map<String ,Object> success = universityIndicatorsService.gxKypt(highName);
        return Result.success(success);
    }
    /**
     * 高校-学校治理KPI指标
     * @param highName 高校名
     * @return 高校指标
     * @author jfh
     * @date 2025-08-28
     */
    @PostMapping("/gxXxzlKpi")
    public Result<Map<String, UniversityIndicatorVo>> gxXxzlKpi(@RequestParam String highName) {
        Map<String, UniversityIndicatorVo> success = universityIndicatorsService.gxXxzlKpi(highName);
        return Result.success(success);
    }

    /**
     * 区县-学校治理KPI指标
     * @param countyName 区县名
     * @return 区县指标
     * @author jfh
     * @date 2025-08-28
     */
    @PostMapping("/qxXxzlKpi")
    public Result<Map<String, UniversityIndicatorVo>> qxXxzlKpi(@RequestParam String countyName) {
        Map<String, UniversityIndicatorVo> success = countyIndicatorsService.qxXxzlKpi(countyName);
        return Result.success(success);
    }

}
